#!/usr/bin/env python

import sys

def printUsage():
    print "Usage"
    print "====="
    print "  %s TestFileName" % sys.argv[0]
    print 
    print "example:"
    print "  %s my_test_file.py - Runs the tests from the given file." % sys.argv[0]
    sys.exit()

def parseArgs():
    try:
        argument = sys.argv[1]
        del sys.argv[1]
        return argument
    except IndexError, e:
        printUsage()

def addPythonPath(path):
    from os.path import normpath
    sys.path.insert(0, normpath(path))

def fixIncludePath():
    # the testoob executable is often in the same directory as testoob
    from os.path import dirname, join
    module_path = join(dirname(__file__), "..")
    addPythonPath(module_path)

argument = parseArgs()
if (argument == "-h" or argument == "--help"):
    printUsage()

try:
    # Add the path of the ran file to the python path, so that includes
    # from the same directory would work.
    from os.path import dirname
    addPythonPath(dirname(argument))

    name = __name__
    __name__ = "".join(argument.split("/")[-1].split(".")[0])
    execfile(argument)
    __name__ = name

    try:
        import testoob
    except ImportError:
        fixIncludePath()
        import testoob
    testoob.main()

except IOError, e:
    print e
    sys.exit(1)
